package bookstore_mall.service.impl;

import bookstore_mall.common.PageVO;
import bookstore_mall.domain.BookCategory;
import bookstore_mall.domain.dto.category.updateCategoryRequest;
import bookstore_mall.exception.BusinessException;
import bookstore_mall.mapper.BookCategoriesMapper;
import bookstore_mall.resp.ResultCodeEnum;
import bookstore_mall.service.BookCategoriesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.UUID;

@Service
public class BookCategoryServiceImpl extends ServiceImpl<BookCategoriesMapper, BookCategory> implements BookCategoriesService {

    /**
     * 按页面查找
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @param name     名称
     * @return 页面vo<图书分类>
     */
    @Override
    public PageVO<BookCategory> findByPage(int page, int pageSize, String name) {
        LambdaQueryWrapper<BookCategory> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            queryWrapper.like(BookCategory::getName, name);
        }
        Page<BookCategory> bookCategoryPage = this.page(new Page<>(page, pageSize), queryWrapper);
        return new PageVO<>(bookCategoryPage.getTotal(), bookCategoryPage.getRecords());
    }

    /**
     * 按类别id查找
     *
     * @param categoryId 类别id
     * @return 图书类别
     */
    @Override
    public BookCategory findByCategoryId(String categoryId) {
        LambdaQueryWrapper<BookCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookCategory::getCategoryId, categoryId);
        BookCategory bookCategory = this.getOne(queryWrapper);
        if(bookCategory == null){
            throw new BusinessException(ResultCodeEnum.CATEGORY_NOT_FOUND);
        }
        return bookCategory;
    }

    /**
     * 按父id查找
     *
     * @param parentId 父id
     * @param page     页面
     * @param pageSize 页面大小
     * @return ipad<图书分类>
     */
    @Override
    public IPage<BookCategory> findByParentId(Integer parentId, int page, int pageSize) {
        LambdaQueryWrapper<BookCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookCategory::getParentCategoryId, parentId);
        return this.page(new Page<>(page, pageSize), queryWrapper);
    }

    /**
     * 创建分类
     *
     * @param category 类别
     * @return 图书类别
     */
    @Override
    @Transactional
    public BookCategory create(BookCategory category) {
        category.setCategoryId(UUID.randomUUID().toString());
        boolean result = this.save(category);
        if(!result){
            throw new BusinessException(ResultCodeEnum.OPERATION_FAIL);
        }
        return category;
    }

    @Override
    @Transactional
    public boolean updateCategory(updateCategoryRequest category, String categoryId) {
        LambdaQueryWrapper<BookCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookCategory::getCategoryId, categoryId);
        BookCategory bookCategory = new BookCategory();
        BeanUtils.copyProperties(category, bookCategory);
        boolean result = this.update(bookCategory, queryWrapper);
        if(!result){
            throw new BusinessException(ResultCodeEnum.OPERATION_FAIL);
        }
        return true;
    }

    @Override
    @Transactional
    public boolean deleteCategory(String categoryId) {
        LambdaQueryWrapper<BookCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookCategory::getCategoryId, categoryId);
        boolean remove = this.remove(queryWrapper);
        if(!remove){
            throw new BusinessException( ResultCodeEnum.OPERATION_FAIL);
        }
        return true;
    }
} 